package com.aigame.socket;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.aigame.entity.Order;
import com.aigame.util.EncryptionTools;
import com.aigame.util.NetTransferUtil;

public class ConsumeSocket extends BaseSocket {


	public String consume(Order order) {
		String url = "http://api.aigame365.com/api/payMoney.shtml";
		paramsMap.put("appid", order.getAppid());
		paramsMap.put("serverid", order.getServerid());
		paramsMap.put("orderid", order.getOrderid());
		paramsMap.put("uid", order.getUid());
		paramsMap.put("money", order.getMoney());
		paramsMap.put("productid", order.getProductid());
		paramsMap.put("productname", order.getProductname());
		String macString = EncryptionTools.md5(order.getAppid()
				+ order.getServerid() + order.getOrderid() + order.getUid()
				+ order.getMoney() + order.getProductid() + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,5,5);
		if(resultString==null) {
			return null;
		}
		Log.i("consume", resultString);
		JSONObject resultObj = null;
		try {
			resultObj = new JSONObject(resultString);
			String result = (String) resultObj.get("result");
			String money = (String) resultObj.get("amount");
			if (result.equals("1")) {
				return money;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	public Boolean checkOrder(Order order) {
		String url = "http://api.aigame365.com/api/queryOrder.shtml";
		paramsMap.put("orderid", order.getOrderid());
		String macString = EncryptionTools.md5(order.getOrderid() + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,2,2);
		if(resultString==null) {
			return false;
		}
		Log.i("checkOrder", resultString);
		JSONObject resultObj = null;
		try {
			resultObj = new JSONObject(resultString);
			String result = (String) resultObj.get("result");
			if (result.equals("1")) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}
	public Order queryOrder(Order order) {
		String url = "http://api.aigame365.com/api/queryOrder.shtml";
		paramsMap.put("orderid", order.getOrderid());
		String macString = EncryptionTools.md5(order.getOrderid() + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,2,2);
		if(resultString==null) {
			return null;
		}
		Log.i("queryOrder", resultString);
		JSONObject resultObj = null;
		try {
			resultObj = new JSONObject(resultString);
			String result = (String) resultObj.get("result");
			if (result.equals("1")) {
				JSONObject info = (JSONObject) resultObj.get("info");
				Order queryOrder = new Order();
				queryOrder.setOrderid( order.getOrderid());
				queryOrder.setUid( info.getString("uid"));
				queryOrder.setMoney( info.getString("money"));
				queryOrder.setProductid( info.getString("productid"));
				queryOrder.setProductname( info.getString("productname"));
				return queryOrder;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
